<?php

use yii\helpers\Html;
use kartik\grid\GridView;
use common\models\business\BusinessBackpig;
use yii\helpers\ArrayHelper;
/* @var $this yii\web\View */
/* @var $searchModel backend\modules\business\models\BusinessBackpigSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '回购单管理';
$this->params['breadcrumbs'][] = $this->title;
?>


<section class="wrapper site-min-height">
    <!-- page start-->
    <section class="panel">
        <header class="panel-heading">
            <?= Html::a('返回','javascript:history.back(-1)'); ?>
            <?= Html::encode($this->title) ?>
        </header>
        <div class="panel-body">
            <div class="adv-table editable-table ">



                <div class="business-backpig-index">

                    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>
                    <?php
                    echo GridView::widget([
                        'dataProvider'=>$dataProvider,
                        'filterModel'=>$searchModel,
                        'showPageSummary'=>true,
                        'pjax'=>false,
                        'striped'=>true,
                        'hover'=>true,
                        'panel'=>['type'=>'primary', 'heading'=>'仔猪管理'],
                        'containerOptions'=>['style'=>'overflow: auto'], // only set when $responsive = false
                        'headerRowOptions'=>['class'=>'kartik-sheet-style'],
                        'filterRowOptions'=>['class'=>'kartik-sheet-style'],
                        'columns'=>[
                            [
                                'attribute'=>'number',
                                'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                                'width'=>'100px',
                            ],
                            [
                                'attribute'=>'farmerid',
                                'format' => 'raw',
                                'value' => function($model){
                                    return $model->farmer?Html::a($model->farmer->name,\yii\helpers\Url::to(['/business/farmers/view','id'=>$model->farmer->id])):'(未设置)';
                                },
                                'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                                'width'=>'100px',
                                'filterType'=>GridView::FILTER_SELECT2,
                                'filter'=>ArrayHelper::map(\common\models\business\BusinessFarmers::find()->asArray()->all(),'id','name'),
                                'filterWidgetOptions'=>[
                                    'pluginOptions'=>['allowClear'=>true],
                                ],
                                'filterInputOptions'=>['placeholder'=>'代养农户','style'=>'width:50px'],
                            ],
                            [
                                'attribute'=>'pigno',
                                'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                                'width'=>'100px',
                            ],
                            [
                                'attribute'=>'grossweight',
                                'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                                'width'=>'100px',
                                'format'=>['decimal', 2],
                                'pageSummary'=>true
                            ],
                            [
                                'attribute'=>'backprice',
                                'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                                'width'=>'100px',
                                'format'=>['decimal', 2],
                                'pageSummary'=>true
                            ],
                            [
                                'attribute'=>'status',
                                'value' => function($model){
                                    if($model->status==BusinessBackpig::STATUS_AGREE) {
                                        return Html::label(BusinessBackpig::$statues[$model->status], '', ['class' => 'btn btn-xs btn-success']);
                                    }else if($model->status==BusinessBackpig::STATUS_DISAGREE) {
                                        return Html::label(BusinessBackpig::$statues[$model->status], '', ['class' => 'btn btn-xs btn-danger']);
                                    }
                                    return Html::label(BusinessBackpig::$statues[$model->status], '', ['class' => 'btn btn-xs btn-default']);
                                },
                                'format' => 'raw',
                                'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                                'width'=>'100px',
                                'filterType'=>GridView::FILTER_SELECT2,
                                'filter'=>BusinessBackpig::$statues,
                                'filterWidgetOptions'=>[
                                    'pluginOptions'=>['allowClear'=>true],
                                ],
                                'filterInputOptions'=>['placeholder'=>'是否通过审核'],
                            ],
                            [
                                'class' => '\kartik\grid\ActionColumn',
                                'deleteOptions' => ['label' => '<i class="glyphicon glyphicon-remove"></i>'],
                                'options' => ['width' => '100px;'],
                                'template' => '{view}{update}{agree}{disagree}',
                                'buttons' => [
                                    'view' => function ($url, $model) {
                                        return Html::a('详情|',\yii\helpers\Url::to(['view','id'=>$model->id,'op'=>'agree']),[
                                            'title' => '详情','class'=>'view','data-id'=>$model->id,
                                        ]);
                                    },
                                    'update' => function ($url, $model) {
                                        return Html::a('更新|',\yii\helpers\Url::to(['update','id'=>$model->id,'op'=>'agree']),[
                                            'title' => '更新','class'=>'update','data-id'=>$model->id,
                                        ]);
                                    },
                                    'agree' => function ($url, $model) {
                                        return Html::a('通过|',\yii\helpers\Url::to(['opagree','id'=>$model->id,'op'=>'agree','pigid'=>$model->pigid]),[
                                            'title' => '恢复','class'=>'recycle-back','data-id'=>$model->id,
                                        ]);
                                    },
                                    'disagree' => function ($url, $model) {
                                        return Html::a('不通过',\yii\helpers\Url::to(['opagree','id'=>$model->id,'op'=>'disagree','pigid'=>$model->pigid]),[
                                            'title' => '恢复','class'=>'recycle-back','data-id'=>$model->id,
                                        ]);
                                    }
                                ],
                            ],

                            [
                                'class' => '\kartik\grid\CheckboxColumn'
                            ]
                        ],
                        'toolbar'=> [
                            [
                                'content' => Html::button('<i class="glyphicon glyphicon-folder-close"> 结算选中</i>', ['type'=>'button', 'title'=>'结算选中', 'class'=>'btn btn-success', 'id'=>'finish'])
                            ],
                            '{export}',
                            '{toggleData}',
                        ],
                        'bordered'=>true,
                        'condensed'=>true,
                    ]);
                    ?>

                </div>
            </div>
        </div>
    </section>
</section>





<script>
    <?php $this->beginBlock('js_end') ?>
    $('#finish').click(function () {
        var keys = jqchk();
        console.log(keys);
        if(keys.length == 0){
            alert('请选择购进单');
            return false;
        }
        ids  = keys.join(',');
        var requestUrl = '<?php echo \yii\helpers\Url::to(['/business/backpig-finish/create'])?>';
        window.location.href=requestUrl+'?'+'ids='+ids
    });

    function jqchk() { //jquery获取复选框值=
        var chk_value = [];
        $('input[name="selection[]"]:checked').each(function () {
            chk_value.push($(this).val());
        });
        return chk_value;
    }
    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>
</script>

